package com.wmlt.api;

import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.wmlt.entity.User;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import com.wmlt.service.UserService;
import com.wmlt.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@WebServlet("/afterlogins")
public class Afterlogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("QQ登陆回调");
        try {
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            String accessToken = null,
                    openID = null;
            long tokenExpireIn = 0L;
            if (accessTokenObj.getAccessToken().equals("")) {
                System.out.print("没有获取到响应参数");
            } else {
                accessToken = accessTokenObj.getAccessToken();
                tokenExpireIn = accessTokenObj.getExpireIn();
                OpenID openIDObj = new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();
                System.out.println(openID);
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
                //生成时间
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
                Date date = new Date(System.currentTimeMillis());
                String time = formatter.format(date);
                //生成uuid
                String uuid = null;
                for (int i = 0; i < 5; i++) {
                    //注意replaceAll前面的是正则表达式
                    uuid = UUID.randomUUID().toString().replaceAll("-", "");
                }

                //创建user对象
                //1:用户唯一ID  2:QQ用户的昵称   3:默认密码123456   4:QQ用户的性别     5:手机号为空      6:QQ用户的唯一ID       7:注册时间      8:QQ用户的头像       9:QQ用户的邮箱   10:用户的余额
                User user = new User(uuid,userInfoBean.getNickname(),"123456", userInfoBean.getGender(), null, openID, time, userInfoBean.getAvatar().getAvatarURL100(),openID+"@qq.com",0);
                UserService userService = new UserServiceImpl();
                userService.qqRegister(user);
                //调用userdao

                HttpSession session = request.getSession();
                session.setAttribute("userInfo", userInfoBean);
                session.setAttribute("name", userInfoBean.getNickname());
                session.setAttribute("qimg", userInfoBean.getAvatar());
                session.setAttribute("avatar", userInfoBean.getAvatar().getAvatarURL100());
                response.sendRedirect("index.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
